package cate.game.activity.specialtraining.po;

import cate.common.table.activity.specialtraining.task.SpecialTrainingTaskRow;
import cate.common.table.activity.specialtraining.task.SpecialTrainingTaskSheet;
import cate.common.table.d.GDObj;
import cate.common.util.GameResult;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.specialtraining.SpecialTrainingActivity;
import cate.game.res.MixRes;
import cate.game.role.task.TaskGroup;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;

import java.util.LinkedList;
import java.util.List;

@NoteClass(value = "特训活动-任务")
public class SpecialTrainingActivityTaskPO extends SubActivitySingle<SpecialTrainingActivity> {

    public void excelCheck() {
        TaskGroup taskGroup = role.getTask().getGroup(funcId());
        SpecialTrainingTaskSheet taskSheet = parent.getTaskSheet();
        if (taskSheet == null) {
            taskGroup.clear();
            return;
        }
        // 删除不存在的任务
        List<Integer> delIds = new LinkedList<>();
        for (int taskId : taskGroup.taskIdSet) {
            SpecialTrainingTaskRow configRow = taskSheet.get(taskId);
            if (configRow == null) {
                delIds.add(taskId);
            }
        }
        if (delIds.size() > 0) {
            taskGroup.removeItemList(delIds);
        }
        // 添加新增的任务
        List<Integer> addIds = new LinkedList<>();
        taskSheet.getList().stream().filter(item->item.day<= parent.getCurrentDay()).forEach(config -> addIds.add(config.id));
        taskGroup.addItemListNoRepeat(addIds);
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        excelCheck();
    }

    public void beforeOver() {

    }

    public void clear() {
        role.getTask().removeGroup(funcId());
    }

    /**
     * 获取任务配置
     *
     * @param taskId 任务ID
     */
    @JsonIgnore
    public SpecialTrainingTaskRow getConfig(int taskId) {
        SpecialTrainingTaskSheet taskSheet = parent.getTaskSheet();
        if (taskSheet == null) {
            return null;
        }
        return taskSheet.get(taskId);
    }

    /**
     * 是否可以领取任务奖励
     *
     * @param taskId 任务ID
     * @return 检测结果 无携带数据
     */
    public GameResult<Void> canGetTaskReward(int taskId) {
        GameResult<Void> r = new GameResult<>();
        SpecialTrainingTaskRow configRow = getConfig(taskId);
        if (configRow == null) {
            return r.fail("任务不存在");
        }
        if (configRow.day > parent.day) {
            return r.fail("任务天数未解锁");
        }
        return r.success();
    }

    /**
     * 领取任务奖励
     *
     * @param rewardRes 奖励资源
     * @return 领取结果 携带实际发放的奖励
     */
    public GameResult<MixRes> onGetTaskReward(MixRes rewardRes) {
        int expNum = (int) rewardRes.count(GDObj.Type.NUM, parent.getPointItemTid());
        if (expNum > 0) {
            parent.pointPO.point += expNum;
        }
        MixRes realRes = rewardRes.copy();
        realRes.items.removeIf(item -> item.type == GDObj.Type.NUM && item.tid == parent.getPointItemTid());
        parent.noticeUpdate();
        return new GameResult<MixRes>().success("success", realRes);
    }
}
